package com.example.project.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/manage/*")
public class ManageLoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        String uri = httpRequest.getRequestURI();

        if(uri.contains("/login"))
        {
            chain.doFilter(request,response);
            return;
        }
        HttpSession session = httpRequest.getSession();
        Object o = session.getAttribute("admin");

        if(o==null)
        {
            String path = httpRequest.getContextPath();
            httpResponse.sendRedirect(path+"/web/login");
        }
        else
        {
            chain.doFilter(request, response);
        }
    }
}
